Thread: conver char [] to char*

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    24

    conver char [] to char*

    hi,
    i got
    Code:
    char word [80]
    and want to convert it to

    Code:
    char *word
    is it possible?

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by sujeet1 View Post
    hi,
    i got
    Code:
    char word [80]
    and want to convert it to

    Code:
    char *word
    is it possible?
    Well *word is a pointer to a char, which can point to any address of a variable of type char. Which basicaly mean that scalar vallue of char * would be set to 1.

    So you could do this

    Code:
     
    char word_str[20] = { "Hello World"};
     
    word = word_str
    So you are making the word to point to word_str. So now then. What would you expect when I type this

    Code:
    printf("%s", word);
    ???

    ssharish

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > is it possible?
    Depends, how were you using the array to begin with?

    Generally, the answer is yes, providing that you arrange for the pointer to point to 80 chars of memory before attempting to use the space (ssharish2005 showed one way).

    However, there are a few instances where a direct replacement is not possible without causing other changes in the program. An example would be using sizeof()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM